package io.wjc.config;

import io.wjc.common.RsaUtils;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.URL;
import java.security.PrivateKey;
import java.security.PublicKey;

@Data
@Component
public class RsaKeyProperties {

    private URL pubKeyFile = RsaKeyProperties.class.getClassLoader().getResource("rsa/id_key_rsa.pub");
    private URL priKeyFile = RsaKeyProperties.class.getClassLoader().getResource("rsa/id_key_rsa");

    private PublicKey publicKey;
    private PrivateKey privateKey;

    @PostConstruct
    public void createRsaKey() throws Exception {
        publicKey = RsaUtils.getPublicKey(pubKeyFile.getFile());
        privateKey = RsaUtils.getPrivateKey(priKeyFile.getFile());
    }

    public PublicKey getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(PublicKey publicKey) {
        this.publicKey = publicKey;
    }

    public PrivateKey getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(PrivateKey privateKey) {
        this.privateKey = privateKey;
    }
}
